<?php

 
class My_Pear
{                                               
    public static function getInstance_ExcelWriter($name = null)
    {
        set_include_path(get_include_path() . PATH_SEPARATOR . LIBRARY_PATH . "/Pear");
        require_once LIBRARY_PATH . '/Pear/Spreadsheet/Excel/Writer.php';
        
        if($name){            
            return new Spreadsheet_Excel_Writer($name);
        }        
        return new Spreadsheet_Excel_Writer();
    }
    public static function getInstance_ExcelReader($name = null)
    {
        set_include_path(get_include_path() . PATH_SEPARATOR . LIBRARY_PATH . "/Pear");
        require_once LIBRARY_PATH . '/Pear/Spreadsheet/Excel/Reader.php';
        
        if($name){
            $data = new Spreadsheet_Excel_Reader();                     
            $data->setOutputEncoding('utf-8');                       
            $data->read($name);                                                
            //error_reporting(E_ALL ^ E_NOTICE);
            
            for ($i = 1; $i <= $data->sheets[0]['numCols']; $i++) {
                $title[]=$data->sheets[0]['cells'][1][$i];                    
            }            
            //tao mang luu content
            for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
                $conten[$i - 2] = array();
                for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {                    
                    if(mb_check_encoding($data->sheets[0]['cells'][$i][$j],'utf-8'))
                        $dataTemp=$data->sheets[0]['cells'][$i][$j];
                    else 
                        $dataTemp=utf8_encode($data->sheets[0]['cells'][$i][$j]);                                                                                
                    $conten[$i - 2][$title[$j-1]] = $dataTemp;
                }
            }                                    
            return $conten;
        }        
        return new Spreadsheet_Excel_Reader();
    }  
    public static function readAndWrite($nameBefore,$nameAfter, $userId,$gameId){
        //doc file excel
        set_include_path(get_include_path() . PATH_SEPARATOR . LIBRARY_PATH . "/Pear/Excel");
        require_once LIBRARY_PATH . '/Pear/Excel/reader.php';
        $data = new Spreadsheet_Excel_Reader();
        $data->setOutputEncoding('UTF-8');
        $data->read(PUBLIC_PATH . '/file/'.$nameAfter);
        error_reporting(E_ALL ^ E_NOTICE);
        //tao mang luu header
        $title = array(
           "Source", "Origin", "Position","Other Link", "Result"
            );
        //tao mang luu content
        for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
            $conten[$i - 2] = array();
            for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
                $dataTemp=strtolower($data->sheets[0]['cells'][$i][$j]);
                $conten[$i - 2][$j - 1] = $dataTemp;
            }
        }
        //ghi file excel
        error_reporting(E_ERROR | E_WARNING | E_PARSE);
        set_include_path(get_include_path() . PATH_SEPARATOR  . LIBRARY_PATH . "/Pear");
        require_once LIBRARY_PATH . '/Pear/Spreadsheet/Excel/Writer.php';
        $workbook=new Spreadsheet_Excel_Writer();
	    $workbook->setVersion(8, 'utf-8'); 
        //format header   
		$header = &$workbook->addFormat(); 
		$header->setFgColor('32'); 
        $header->setColor('white');
		$header->setAlign('left');
		$header->setHAlign('left'); 
		$header->setVAlign('left'); 
		$header->setBorder(1); 	
        $header->setTextWrap();
        //format header cua cot position
       	$header1 = &$workbook->addFormat(); 
		$header1->setAlign('center');
		$header1->setHAlign('center'); 
		$header1->setVAlign('center'); 
        $header1->setFgColor('32'); 
        $header1->setColor('white');
		$header1->setBorder(1); 	
        $header1->setTextWrap();
        //format content
       	$content = &$workbook->addFormat(); 
		$content->setAlign('left');
		$content->setHAlign('left'); 
		$content->setVAlign('left'); 
		$content->setBorder(1); 	
        $content->setTextWrap();
        
        //format error
        $error = &$workbook->addFormat(); 
		$error->setAlign('left');
		$error->setHAlign('left'); 
		$error->setVAlign('left'); 
        $error->setColor('red');
		$error->setBorder(1); 	
        $error->setTextWrap();
        //format content cua cot position
       	$content1 = &$workbook->addFormat(); 
		$content1->setAlign('center');
		$content1->setHAlign('center'); 
		$content1->setVAlign('center'); 
		$content1->setBorder(1); 	
        $content1->setTextWrap();
              
		$worksheet1 = &$workbook->addWorksheet($params['origin']); 
        $worksheet1->setInputEncoding('utf-8');
        //set chieu rong cua col tu col thu may toi tnu may va co size bao nhieu
        $worksheet1->setColumn(0, 2, 15);  
        $worksheet1->setColumn(3, 3, 60); 
        $worksheet1->setColumn(4, 4, 60);
        //lap mang de luu file excel
        foreach($title as $keyTit => $valTit){
            if($keyTit==2){
                $worksheet1->write(0, $keyTit, $valTit, $header1);
            }
            $worksheet1->write(0, $keyTit, $valTit, $header);
        }
        //kiem tra co ton tai file config wl.ini hay khong?
        if (is_file(APPLICATION_PATH . '/configs/wl.ini')) {
            $gameName="hai-tac-2";
            $gameConf = new Zend_Config_Ini(APPLICATION_PATH . '/configs/wl.ini',APPLICATION_ENV);
        }
        else{    
            break;
        }
        $modelAds = new Default_Model_Ads();
        foreach($conten as $keyPar => $valPar){
            //them vao col result
            if($valPar[0]==null || $valPar[1]==null || $valPar[2]==null ){
                $valPar[]="ERROR: Thiếu thông tin để tạo link";
            }
            else if (isset($gameConf->games->$gameName->link->$valPar[1])){   
                if($valPar[3]==null){
                    $valPar[]=$gameConf->games->$gameName->link->$valPar[1]."?source=".$valPar[0]."_".$valPar[2];
                }
                else{
                    $parts = parse_url($valPar[3]);
                    $valPar[]=$gameConf->games->$gameName->link->$valPar[1]."?source=".$valPar[0]."_".$valPar[2]."&".$parts['query'];
                }
            }
            else{
                $valPar[]="ERROR: Không tìm thấy vị trí đón";
            }
            //luu data vao bang ads
            try{
                if(strlen(strstr($valPar[4],'ERROR')) >0){  
                }
                else{
                    $adsId = $modelAds->addData($valPar[0], $valPar[1], $valPar[2], $valPar[3], $valPar[4], time(), $userId, $gameId);
                } 
            }
            catch(Exception $e){
                //pr($e->getMessage());
            }            
             
           // $valPar[]="http://vht.game5.vn/".$valPar[1]."/?source=".$valPar[0]."_".$valPar[2];
            foreach($valPar as $keyItems => $valItems){
                if($keyItems==2){
                    $worksheet1->write($keyPar+1,$keyItems,$valItems, $content1);
                }
                if(strlen(strstr($valItems,'ERROR')) > 0 ){
                    $worksheet1->write($keyPar+1,$keyItems,$valItems, $error);
                }
                //dong,cot,giatri,format
                $worksheet1->write($keyPar+1,$keyItems,$valItems, $content);
            }
        }
		$workbook->send($nameBefore); 
		$workbook->close();
        die;
    } //end function
                         
}
